import wave
import uuid

def save_audio_file(audio_data: bytearray) -> str:
    audio_file_path = f"/tmp/{uuid.uuid4()}.wav"
    
    # 将接收到的PCM数据写入wav文件
    with wave.open(audio_file_path, 'wb') as wav_file:
        wav_file.setnchannels(1)
        wav_file.setsampwidth(2)  # 16 bits per sample
        wav_file.setframerate(16000)
        wav_file.writeframes(audio_data)

    # 检查文件是否为有效的wav文件
    try:
        with wave.open(audio_file_path, "rb") as wav_file:
            wav_file.readframes(1)  # 读取一帧以检查文件是否有效
    except wave.Error as e:
        raise ValueError("文件不是有效的wav格式") from e
    
    return audio_file_path
